import { getCurrentAudioLesson, getPrevInfo, getNextInfo, setCurrentPlayList } from '../../../common/service/courses-service';
import { GLOBAL_STORE_TYPE_STORAGE } from '../../../common/constants';
import { eventBusGetter } from '../../../utils/eventbus';

Page({
    data: {
        isShow: false,
        audioInfo: {}, // 正在播放的音频详细数据
        audioList: [
            {
                id: 1,
                title: '童年',
                singer: '罗大佑',
                coverImgUrl: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1521455810455&di=e3b29b1f89ac894901915f7381e70e56&imgtype=0&src=http%3A%2F%2Fimg.go007.com%2F2017%2F03%2F08%2Fff4bba65b49231a4_0.jpg',
                src: 'http://p5ttxd7wz.bkt.clouddn.com/%E7%BD%97%E5%A4%A7%E4%BD%91%20-%20%E7%AB%A5%E5%B9%B4%20-%20live.mp3'
            },
            {
                id: 2,
                title: '此时此刻',
                singer: '许巍',
                coverImgUrl: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000',
                src: 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46'
            },
            {
                id: 3,
                title: '小苹果',
                singer: '筷子兄弟',
                coverImgUrl: 'http://aliyunimg.9ku.com/pic/zjpic/15/141351.jpg?x-oss-process=image/resize,m_fill,w_150,h_150,limit_0/auto-orient,0',
                src: 'http://p5ttxd7wz.bkt.clouddn.com/%E5%B0%8F%E8%8B%B9%E6%9E%9C.m4a'
            },
            {
                id: 4,
                title: 'As Long as You Love Me',
                singer: 'Backstreet Boys',
                coverImgUrl: 'http://p1.music.126.net/P-2uAbQrgtIZfYy8HxuY2g==/19069929672343699.jpg?param=130y130',
                src: 'http://p5ttxd7wz.bkt.clouddn.com/As%20Long%20as%20You%20Love%20Me.mp3'
            },
            {
                id: 5,
                title: 'Billie Jean',
                singer: 'Michael Jackson',
                coverImgUrl: 'http://p1.music.126.net/zKHEOyp8xUuNPPulBflEjw==/5648191231956170.jpg?param=130y130',
                src: 'http://p5ttxd7wz.bkt.clouddn.com/Beat%20It.mp3'
            }
        ]
    },
    onShareAppMessage: function () {
        return {
            title: '邀你一起学：孙子兵法',
            path: '/pages/index/index'
        };
    },
    onLoad: function (e) {
        console.log('list onLoad');
        eventBusGetter().$on('eventPlay', json => {
            console.log('播放页面', JSON.stringify(json));
            this.setData({
                audioInfo: json.audioInfo
            });
        });
    },
    onShow: function (e) {
        let lessonInfo = getCurrentAudioLesson();
        if (lessonInfo) {
            this.setData({
                audioInfo: lessonInfo
            });
        }

        setCurrentPlayList(JSON.parse(JSON.stringify(this.data.audioList)), {
            storageType: GLOBAL_STORE_TYPE_STORAGE
        }).then((res) => {
            console.log(res);
        });
    },
    onHide: function (e) {
        // console.log('--------------------', this.data.audioInfo);
        // console.log('list onHide。修改globalData:', globalData('audioInfo'));
    },
    onTabItemTap: function (e) {
        console.log('list onTabItemTap');
    },
    handleShare: function () {
        console.log('handleShare');
        // wx.showShareMenu({
        //     withShareTicket: true
        // });
        this.onShareAppMessage();
    },
    playAudio: function (e) {
        let _data = this.data;
        let objAudio;
        if (typeof e === 'number') {
            let index = _data.audioList.findIndex((item) => {
                return item.id === e;
            });

            objAudio = _data.audioList[index + 1];
        } else if (typeof e === 'object' && e.currentTarget) {
            let dataSet = e.currentTarget.dataset;
            objAudio = _data.audioList[dataSet.index];
        }

        objAudio && this.setData({
            audioInfo: objAudio
        });
    },
    playEnded: function (e) {
        let objAudio = e.detail.currentAudio || {};
        console.log('播放结束，下一首', JSON.stringify(e.detail));
        this.playNext(objAudio.id || 0);
    },
    playPrev: function (e) {
        console.log('上一首');
        let currentLesson = e.detail.currentAudio;
        let obj = getPrevInfo(currentLesson.id);
        if (obj) {
            // 重置播放器
            this.setData({
                audioInfo: obj
            });
        }
        console.log('');
    },
    playNext: function (e) {
        console.log('下一首');
        let lessonId;
        let currentLesson;

        if (typeof e === 'number') {
            // 直接调用
            lessonId = e;
        } else {
            // 事件触发
            currentLesson = e.detail.currentAudio;
            lessonId = currentLesson.id;
        }
        let obj = getNextInfo(lessonId);
        if (obj) {
            // 重置播放器
            this.setData({
                audioInfo: obj
            });
        }
    },
    goIndex: function () {
        wx.navigateTo({url: '/pages/index/index'});
    },
    goPlayList: function () {
        wx.navigateTo({ url: '/pages/test/audio-list/audio-list' });
    }
});
